package com.itheima.canal.listener;


import com.alibaba.otter.canal.protocol.CanalEntry;
import com.itheima.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@CanalEventListener
public class SpuListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @ListenPoint(schema = "changgou_goods",table = "tb_spu")
    public void goodsUp(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        Map<String,String> oldData=new HashMap<>();
        rowData.getBeforeColumnsList().forEach(column -> oldData.put(column.getName(),column.getValue()));


        Map<String,String> newData=new HashMap<>();
        rowData.getAfterColumnsList().forEach(column -> newData.put(column.getName(),column.getValue()));

        if (oldData.get("is_marketable").equals("0")&&newData.get("is_marketable").equals("1")){
            rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_UP_EXCHANGE,"",newData.get("id"));
        }

        if (oldData.get("is_marketable").equals("1")&&newData.get("is_marketable").equals("0")){
            rabbitTemplate.convertAndSend(RabbitMQConfig.GOODS_DOWN_EXCHANGE,"",newData.get("id"));
        }

    }
}
